package com.Xht.www.dao;

import com.Xht.www.config.CustomDatabaseConnectionPool;
import java.sql.Connection;

public abstract class BaseDAO {

    // 获取连接池单例实例
    private static final CustomDatabaseConnectionPool connectionPool = CustomDatabaseConnectionPool.getInstance();

    // 获取数据库连接
    protected Connection getConnection() {
        return connectionPool.getConnection();
    }

    // 释放数据库连接
    protected void releaseConnection(Connection connection) {
        if (connection != null) {
            connectionPool.releaseConnection(connection);
        }
    }

    // 测试主方法
    public static void main(String[] args) {
        BaseDAO dao = new BaseDAO() {}; // 匿名子类用于测试

        Connection conn = null;
        try {
            conn = dao.getConnection();
            System.out.println("成功获取数据库连接！");
        } catch (Exception e) {
            System.err.println("获取数据库连接失败：" + e.getMessage());
        } finally {
            dao.releaseConnection(conn);
        }
    }
}




